package com.jdd.modules.parking.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.util.AsynRestTmplateUtils;
import com.jdd.common.util.DateUtil;
import com.jdd.common.util.DateUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.order.entity.ParkOrderInfo;
import com.jdd.modules.order.mapper.ParkOrderInfoMapper;
import com.jdd.modules.parking.entity.ParkCarEnterLog;
import com.jdd.modules.parking.entity.ParkCarOutEntranceRecord;
import com.jdd.modules.parking.entity.ParkCarOutLog;
import com.jdd.modules.parking.mapper.ParkCarEnterLogMapper;
import com.jdd.modules.parking.mapper.ParkCarOutEntranceRecordMapper;
import com.jdd.modules.parking.mapper.ParkCarOutLogMapper;
import com.jdd.modules.parking.service.IParkCarOutEntranceRecordService;
import com.jdd.modules.parking.vo.CarParkingInfoVO;
import com.jdd.modules.parking.vo.ParkCarOutEntranceRecordVO;
import com.jdd.modules.parking.vo.ParkOrderInfoSumVo;
import com.jdd.modules.parking.vo.QueryPageDetailsListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 停车记录
 * @Author: jdd
 * @Date:   2022-05-28
 * @Version: V1.0
 */
@Service
@Slf4j
public class ParkCarOutEntranceRecordServiceImpl extends ServiceImpl<ParkCarOutEntranceRecordMapper, ParkCarOutEntranceRecord> implements IParkCarOutEntranceRecordService {
    @Resource
    ParkCarOutEntranceRecordMapper parkCarOutEntranceRecordMapper;
    @Resource
    ParkOrderInfoMapper parkOrderInfoMapper;
    @Resource
    ParkCarEnterLogMapper parkCarEnterLogMapper;
    @Resource
    ParkCarOutLogMapper parkCarOutLogMapper;
    @Resource
    AsynRestTmplateUtils asynRestTmplateUtils;
    /**
     * @Description  根据入场记录组装停车记录
     * @author shangyue
     * @Date 2022-05-28
     * @param carEnterLog
     * @return
     */

    @Override
    public ParkCarOutEntranceRecord assemblyCarEntranceRecord(ParkCarEnterLog carEnterLog) {
        log.info("组装停车记录carEnterLog-》{}",carEnterLog);

        ParkCarOutEntranceRecord parkCarOutEntranceRecord = new ParkCarOutEntranceRecord();
        BeanUtil.copyProperties(carEnterLog,parkCarOutEntranceRecord);
         parkCarOutEntranceRecord.setId(SnowFlakeUtil.getId()).
                 //入场时间
                 setCarEnterTime(carEnterLog.getCarEnterTime()).
                 //车辆性质
                 setCarNature(carEnterLog.getCarNature()).
                 //车辆类型
                 setCarTypeNo(carEnterLog.getCarTypeNo()).
                 setEntArmName(carEnterLog.getArmName()).
                 //批次号
                 setBatchNumber(carEnterLog.getBatchNumber()).
                 //入场记录ID
                 setCarEnterId(carEnterLog.getId()).
                 setSynchronousStatus("N").
                 //入口设备序列号
                 setEnterSerialNo(carEnterLog.getSerialNo()).
                 //入口类型
                 setEnterType(carEnterLog.getEnterType()).
                 //工作站名称
                 setEnterWorkName(carEnterLog.getWorkName()).
                 //操作人姓名
                 setEnterSysUserName(carEnterLog.getSysUserName());
               this.save(parkCarOutEntranceRecord);
        assembleEntityCarParkingInfoVO(parkCarOutEntranceRecord);
        return parkCarOutEntranceRecord;
    }

    @Override
    public void assemblyCarOutEntranceRecord(ParkCarOutLog parkCarOutLog)   {
        log.info("组装停车记录parkCarOutLog-》{}",parkCarOutLog);
        String carEnterId = parkCarOutLog.getCarEnterId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("car_enter_id",carEnterId);
        queryWrapper.orderByDesc();
        List<ParkCarOutEntranceRecord> list = parkCarOutEntranceRecordMapper.selectList(queryWrapper);
        if (list.isEmpty() || null==list){
            log.error("停车记录组装未找到入场记录");
            return;
        }


        ParkCarOutEntranceRecord parkCarOut = list.get(0);

        parkCarOut.setCarOutTime(DateUtil.fomatDateTimeStr(new Date()));
        parkCarOut.setOuterType(parkCarOutLog.getOuterType());
        parkCarOut.setOutSysUserName(parkCarOutLog.getSysUserName());
        parkCarOut.setOuterSerialNo(parkCarOutLog.getSerialNo());
        parkCarOut.setOutWorkName(parkCarOutLog.getWorkName());
        parkCarOut.setOutArmName(parkCarOutLog.getArmName());
        if (StringUtils.isNotBlank(parkCarOut.getCarEnterTime()) && StringUtils.isNotBlank(parkCarOut.getCarOutTime())){
            Date carEnterTime = null;
            Date carOutTime=null;
            try {
                carEnterTime = DateUtils.stringToDate(parkCarOut.getCarEnterTime(), "yyyy-MM-dd HH:mm:ss");
                carOutTime = DateUtils.stringToDate(parkCarOut.getCarOutTime(), "yyyy-MM-dd HH:mm:ss");

            } catch (ParseException e) {
                e.printStackTrace();
            }
            parkCarOut.setChargeDuration( (int) DateUtils.timeDiff2(carEnterTime,carOutTime));
        }
        //根据入场记录ID获取订单信息
        List<ParkOrderInfo> orderByCarEnterId = parkOrderInfoMapper.getOrderByCarEnterId(carEnterId);
        if (CollectionUtil.isNotEmpty(orderByCarEnterId) ){
            ParkOrderInfo parkOrderInfo = orderByCarEnterId.get(0);
            parkCarOut.setChargeTotalFee(parkOrderInfo.getChargeTotalFee());
            parkCarOut.setCouponAmt(parkOrderInfo.getCouponAmt());
            parkCarOut.setChargePaid(chargePaidSum(parkOrderInfo.getChargePaid(),parkOrderInfo.getChargePrepaid()));
            parkCarOut.setChargeDuration(parkOrderInfo.getChargeDuration());
        }
        parkCarOut.setSynchronousStatus("N");
        this.updateById(parkCarOut);
        assembleEntityCarParkingInfoVO(parkCarOut);
    }
    /**
     * @Description
     * @author shangyue
     * @Date 2022-06-07
     * @param chargePaid   已支付费用
     * @param chargePrepaid  预付费用
     * @return
     */

    private String chargePaidSum(String chargePaid,String chargePrepaid){
        if (StringUtils.isBlank(chargePaid)){
            chargePaid="0";
        }
        if (StringUtils.isBlank(chargePrepaid)){
            chargePrepaid="0";
        }
      int sum=  Integer.parseInt(chargePaid)+Integer.parseInt(chargePrepaid);
        return String.valueOf(sum);
    }
    @Override
    public ParkOrderInfoSumVo getParkCarOutEntranceRecordSumInfo(ParkCarOutEntranceRecord parkCarOutEntranceRecord,String entStartTime, String entEndTime, String outStartTime, String outEndTime) {
        ParkCarOutEntranceRecordVO parkCarOutEntranceRecordVO = new ParkCarOutEntranceRecordVO();
        BeanUtil.copyProperties(parkCarOutEntranceRecord,parkCarOutEntranceRecordVO);
        parkCarOutEntranceRecordVO.setOutEndTime(outEndTime);
        parkCarOutEntranceRecordVO.setOutStartTime(outStartTime);
        parkCarOutEntranceRecordVO.setEntEndTime(entEndTime);
        parkCarOutEntranceRecordVO.setEntStartTime(entStartTime);
        ParkOrderInfoSumVo parkCarOutEntranceRecordSumInfo = parkCarOutEntranceRecordMapper.getParkCarOutEntranceRecordSumInfo( parkCarOutEntranceRecordVO);
        if (null==parkCarOutEntranceRecordSumInfo){
            parkCarOutEntranceRecordSumInfo = new ParkOrderInfoSumVo();
            parkCarOutEntranceRecordSumInfo.setChargePaid("0");
            parkCarOutEntranceRecordSumInfo.setCouponAmt("0");
            parkCarOutEntranceRecordSumInfo.setChargeTotalFee("0");
            parkCarOutEntranceRecordSumInfo.setRefundFee("0");
            parkCarOutEntranceRecordSumInfo.setDeductionFee("0");
        }
        return  parkCarOutEntranceRecordSumInfo;
    }

    @Override
    public IPage<QueryPageDetailsListVO> queryPageDetailsList(Page<QueryPageDetailsListVO> page, String batchNumber, String carEnterId) {

        return parkCarOutLogMapper.selectCarEnterIdRecord(page,batchNumber,carEnterId);
    }

    @Override
    public void assemblyCarOutEntranceOrderInfoRecord(ParkOrderInfo parkOrderInfo) {

        List<ParkCarOutEntranceRecord>list =  parkCarOutEntranceRecordMapper.selectCarEnterIdList(parkOrderInfo.getCarEnterId());
        if (CollectionUtil.isNotEmpty(list) ){
            ParkCarOutEntranceRecord parkCarOutEntranceRecord = list.get(0);
            parkCarOutEntranceRecord.setChargeTotalFee(parkOrderInfo.getChargeTotalFee());
            parkCarOutEntranceRecord.setCouponAmt(parkOrderInfo.getCouponAmt());
            parkCarOutEntranceRecord.setChargePaid(chargePaidSum(parkOrderInfo.getChargePaid(),parkOrderInfo.getChargePrepaid()));
            parkCarOutEntranceRecord.setChargeDuration(parkOrderInfo.getChargeDuration());
            parkCarOutEntranceRecord.setSynchronousStatus("N");
            this.updateById(parkCarOutEntranceRecord);
            assembleEntityCarParkingInfoVO(parkCarOutEntranceRecord);
        }
    }
    /**
     * @Description 根据主键修改同步状态
     * @author shangyue
     * @Date 2022-06-21
     * @param id
     * @param synchronousStatus
     * @return
     */
    @Override
    public void updateSynchronousStatus(String id, String synchronousStatus) {
        parkCarOutEntranceRecordMapper.updateSynchronousStatus(id,synchronousStatus);
    }
    /**
     * @Description  获取同步失败的停车记录
     * @author shangyue
     * @Date 2022-06-22
     * @param
     * @return
     */

    @Override
    public List<ParkCarOutEntranceRecord> parkCarOutEntranceRecordListStateN() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("synchronous_status","N");
        return this.list(queryWrapper);
    }

    @Override
    public CarParkingInfoVO assembleEntityCarParkingInfoVO(ParkCarOutEntranceRecord parkCarOutEntranceRecord){
            CarParkingInfoVO carParkingInfoVO = new CarParkingInfoVO();
             carParkingInfoVO.setId(parkCarOutEntranceRecord.getId());
             carParkingInfoVO.setParkCode(parkCarOutEntranceRecord.getParkCode());
             carParkingInfoVO.setParkName(parkCarOutEntranceRecord.getParkName());
             carParkingInfoVO.setCarNature(parkCarOutEntranceRecord.getCarNature());
             carParkingInfoVO.setBatchNumber(parkCarOutEntranceRecord.getBatchNumber());
             carParkingInfoVO.setChargeDuration(parkCarOutEntranceRecord.getChargeDuration());
             carParkingInfoVO.setChargePaid(parkCarOutEntranceRecord.getChargePaid());
             carParkingInfoVO.setChargeTotalFee(parkCarOutEntranceRecord.getChargeTotalFee());
             carParkingInfoVO.setRejectAmt(parkCarOutEntranceRecord.getRefundFee());
             carParkingInfoVO.setCouponAmt(parkCarOutEntranceRecord.getCouponAmt());
             carParkingInfoVO.setDeductAmount(parkCarOutEntranceRecord.getDeductionFee());
             carParkingInfoVO.setPlateNo(parkCarOutEntranceRecord.getPlateNo());
             carParkingInfoVO.setEnterTime(parkCarOutEntranceRecord.getCarEnterTime());
             carParkingInfoVO.setEnterSerialNo(parkCarOutEntranceRecord.getEnterSerialNo());
             carParkingInfoVO.setEnterType(parkCarOutEntranceRecord.getEnterType());
             carParkingInfoVO.setEnterArmName(parkCarOutEntranceRecord.getEntArmName());
             carParkingInfoVO.setEnterWorkName(parkCarOutEntranceRecord.getEnterWorkName());
             carParkingInfoVO.setEnterSysUserName(parkCarOutEntranceRecord.getEnterSysUserName());
             carParkingInfoVO.setOuterType(parkCarOutEntranceRecord.getOuterType());
             carParkingInfoVO.setOutTime(parkCarOutEntranceRecord.getCarOutTime());
             carParkingInfoVO.setOutSerialNo(parkCarOutEntranceRecord.getOuterSerialNo());
             carParkingInfoVO.setOutArmName(parkCarOutEntranceRecord.getOutArmName());
             carParkingInfoVO.setOutWorkName(parkCarOutEntranceRecord.getOutWorkName());
             carParkingInfoVO.setOutSysUserName(parkCarOutEntranceRecord.getOutSysUserName());
            asynRestTmplateUtils.uploadParkCarOutEntranceRecord(carParkingInfoVO);
        return carParkingInfoVO;
    }

}
